﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Model;
using eShop.Business;
using eShop.Utility;

public partial class widget_shopping_cart : System.Web.UI.UserControl
{

    public bool IsEditable
    {
        set
        {
            _isEditable = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    protected void BindData()
    {
        if (ShoppingCartManager.HasAnyItemInCart())
        {
            Cart shoppingCart = ShoppingCartManager.GetCart();
            rShoppingCart.DataSource = shoppingCart.CartItems;
            rShoppingCart.DataBind();
            phCart.Visible = true;
            phEmptyCart.Visible = false;
        }
        else
        {
            phCart.Visible = false;
            phEmptyCart.Visible = true;
        }
    }

    protected void rShoppingCart_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            CheckBox cbxSelectAll = (CheckBox)e.Item.FindControl("cbxSelectAll");
            if (cbxSelectAll != null)
                cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + rShoppingCart.ClientID + "','" + cbxSelectAll.ClientID + "','cbxSelect')");

            Button bItemRemove = (Button)e.Item.FindControl("bItemRemove");
            bItemRemove.Visible = _isEditable;
        }
        else if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CartItem cartItem = (CartItem)e.Item.DataItem;
            if (cartItem.Product.ProductId > 0)
            {
                HiddenField hfProductId = (HiddenField)e.Item.FindControl("hfProductId");
                hfProductId.Value = cartItem.Product.ProductId.ToString();

                HiddenField hfStockId = (HiddenField)e.Item.FindControl("hfStockId");
                hfStockId.Value = cartItem.Stock.StockId.ToString();

                Image iProductImage = (Image)e.Item.FindControl("iProductImage");
                iProductImage.ImageUrl = ImageResizer.CheckImageMissing(cartItem.Product.ProductImageSmallThumbnail, eShop.Business.SiteSettings.NoImageSmallThumbnailUrl);

                HyperLink hlProductName = (HyperLink)e.Item.FindControl("hlProductName");
                hlProductName.NavigateUrl = cartItem.Product.ProductUrl;
                hlProductName.Text = cartItem.Product.ProductName;

                Label lItemNumber = (Label)e.Item.FindControl("lItemNumber");
                lItemNumber.Text = "Item Number: " + cartItem.Product.ItemNumber;

                Label lUnitPrice = (Label)e.Item.FindControl("lUnitPrice");
                lUnitPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.UnitPrice);

                TextBox tbQuantity = (TextBox)e.Item.FindControl("tbQuantity");
                tbQuantity.Text = cartItem.Quantity.ToString();

                Label lQuantity = (Label)e.Item.FindControl("lQuantity");
                lQuantity.Text = cartItem.Quantity.ToString();

                Label lTotalPrice = (Label)e.Item.FindControl("lTotalPrice");
                lTotalPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.TotalPrice);

                Button bItemUpdate = (Button)e.Item.FindControl("bItemUpdate");
                bItemUpdate.CommandArgument = e.Item.ItemIndex.ToString();

                CheckBox cbSelect = (CheckBox)e.Item.FindControl("cbSelect");

                lQuantity.Visible = !(tbQuantity.Visible = bItemUpdate.Visible = cbSelect.Visible = _isEditable);
            }
        } else if (e.Item.ItemType == ListItemType.Footer)
        {
            CartItemCollection cartItems = (CartItemCollection)rShoppingCart.DataSource;
            Label lSubTotal = (Label)e.Item.FindControl("lSubTotal");
            lSubTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItems.SubTotal);
        }
    }

    protected void rShoppingCart_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "UpdateCart")
            {
                int rowIndex;
                if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
                {
                    RepeaterItem rRow = rShoppingCart.Items[rowIndex];
                    if (rRow != null)
                    {
                        int productId, stockId, quantity;
                        TextBox tbQuantity = (TextBox)rRow.FindControl("tbQuantity");
                        HiddenField hfProductId = (HiddenField)rRow.FindControl("hfProductId");
                        HiddenField hfStockId = (HiddenField)rRow.FindControl("hfStockId");

                        int.TryParse(tbQuantity.Text.ToString().Trim(), out quantity);
                        int.TryParse(hfProductId.Value.ToString(), out productId);
                        int.TryParse(hfStockId.Value.ToString(), out stockId);

                        if ( quantity>0 && productId>0 && stockId>0)
                        {
                            ShoppingCartManager.UpdateProductInCart(productId,stockId, quantity);
                            BindData();
                        }
                        else
                        {
                            //uccResponseMessage.SetFailMessage(ApplicationMessage.ShoppingCartInvalidQuantity);
                            //uccResponseMessage.Visible = true;
                        }
                    }
                }
            }
        }
        catch (Exception error)
        {
            //uccResponseMessage.SetFailMessage(ApplicationMessage.CommonFailure);
            //uccResponseMessage.Visible = true;
        }
    }

    protected void bItemRemove_Click(object sender, EventArgs e)
    {
        try
        {
            BindData();
            List<CartItem> removeCartItems = new List<CartItem>();
            foreach (RepeaterItem row in rShoppingCart.Items)
            {
                if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox cbSelect = (CheckBox)row.FindControl("cbSelect");
                    if (cbSelect != null && cbSelect.Checked)
                    {
                        int productId, stockId;

                        HiddenField hfProductId = (HiddenField)row.FindControl("hfProductId");
                        HiddenField hfStockId = (HiddenField)row.FindControl("hfStockId");

                        int.TryParse(hfProductId.Value.ToString(), out productId);
                        int.TryParse(hfStockId.Value.ToString(), out stockId);

                        if (productId > 0 && stockId > 0)
                        {
                            ShoppingCartManager.RemoveProductFromCart(productId, stockId);
                        }
                    }
                }
            }
            BindData();
        }
        catch (Exception error)
        {
            //uccResponseMessage.SetFailMessage(ApplicationMessage.CommonFailure);
            //uccResponseMessage.Visible = true;
        }
    }

    protected void imbContinueShopping_Click1(object sender, EventArgs e)
    {
        Response.Redirect(CustomSiteMap.Home);
    }


    private bool _isEditable;
}